<?php

/**
 * Created by PhpStorm.
 * User: liuxiu
 * Date: 2022/7/14
 * Time: 18:32
 */

namespace business\elasticsearch\services;


use business\elasticsearch\models\BigDataProjectLifeCycleModel;

class BigDataProjectLifeBakCycleService
{

    public function subscript(
        $area,
        $landType,
        $takeLandDate,
        $workStartDate,
        $isHighLevelProject,
        $takeLandPriceLower,
        $takeLandPriceUpper,
        $page,
        $pageSize
    ) {
        $condition = $this->subscriptCondition(
            $area,
            $landType,
            $takeLandDate,
            $workStartDate,
            $isHighLevelProject,
            $takeLandPriceLower,
            $takeLandPriceUpper
        );
        //var_dump(json_encode($condition));
        $query     = BigDataProjectLifeCycleModel::find()
            ->query($condition);

        $query->orderBy(['created_at' => SORT_DESC]);

        $total = $query->count();

        //分页处理
        $query->offset(($page - 1) * $pageSize)->limit($pageSize);
        $data = $query->asArray()->all();

        $result = ['total' => $total, 'list' => []];
        foreach ($data as $dv) {
            $tmp                = $dv['_source'];
            if (!empty($dv['highlight'])) {
                foreach ($dv['highlight'] as $key => $value) {
                    $tmp[$key . 'HL'] = isset($value[0]) ? $value[0] : '';
                }
            }
            $result['list'][] = $tmp;
        }

        return $result;
    }

    public function subscriptCondition(
        $area,
        $landType,
        $takeLandDate,
        $workStartDate,
        $isHighLevelProject,
        $takeLandPriceLower,
        $takeLandPriceUpper
    ) {
        $query = [];
        $slop = !empty(\Yii::$app->params['CommonDataDeveloperSlop']) ?  \Yii::$app->params['CommonDataDeveloperSlop'] : 2;
        $must = [];
        $query['function_score']['query']['bool']['must'] = &$must;

        if (!empty($keyword)) {
            $should = [];
            $must[] = ['bool' => ['should' => &$should]];

            foreach ($keyword as $k) {
                $should[] = [
                    'match_phrase' => ['title' => ["query" => $k, 'slop' => $slop]]
                ];
                $should[] = [
                    'match_phrase' => ['bidding_service_category' => ["query" => $k, "boost" => 3, 'slop' => $slop]]
                ];
                $should[] = [
                    'match_phrase' => ['developer_name' => ["query" => $k, "boost" => 3, 'slop' => $slop]]
                ];
                $should[] = [
                    'nested' => [
                        'path' => 'projects',
                        'query' => ['match_phrase' => ['projects.project_name' => $k]]
                    ]
                ];
            }
        }

        $typeShould = [];
        $must[] = ['bool' => ['should' => &$typeShould]];
        $landMust = [["term" => ['source_type' => '拿地']]];
        $workMust = [["term" => ['source_type' => '开工']]];
        $typeShould[] = ['bool' => ['must' => &$landMust]];
        $typeShould[] = ['bool' => ['must' => &$workMust]];

        if (!empty($area)) {
            $shouldArea = [];
            $must[] = ['bool' => ['should' => &$shouldArea]];
            $shouldArea[] = ["terms" => ['province_code' => $area]];
            $shouldArea[] = ["terms" => ['city_code' => $area]];
        }

        if (!empty($landType)) {
            $landMust[] = ["terms" => ['land_usage_label' => $landType]];
        }

        if (!empty($takeLandDate)) {
            $takeLandDateRange = [];
            $takeLandDateRange['from'] = date('Y-m-d', strtotime($takeLandDate) - 7 * 86400);
            $takeLandDateRange['to'] = date('Y-m-d H:i:s', strtotime($takeLandDate));
            $landMust[] = ['range' => ['take_land_date' => $takeLandDateRange]];
        }

        if (!empty($workStartDate)) {
            $workStartDateRange = [];
            $workStartDateRange['from'] = date('Y-m-d', strtotime($workStartDate) - 7 * 86400);
            $workStartDateRange['to'] = date('Y-m-d H:i:s', strtotime($workStartDate));
            $workMust[] = ['range' => ['start_time' => $workStartDateRange]];
        }

        if (!empty($takeLandPriceLower) || !empty($takeLandPriceUpper)) {
            $priceRange = [];
            if (!empty($takeLandPriceLower)) {
                $priceRange['gte'] = $takeLandPriceLower;
            }
            if (!empty($takeLandPriceUpper)) {
                $priceRange['lt'] = $takeLandPriceUpper;
            }
            if (!empty($priceRange)) {
                $landMust[] = ['range' => ['take_land_price' => $priceRange]];
            }
        }

        if (!empty($isHighLevelProject)) {
            $landMust[] = ['term' => ['high_project_customers_label' => '高端项目客群']];
        }

        return $query;
    }
}
